// 示例 1：

// 输入：nums = [1, 1, 1], k = 2
// 输出：2
// 示例 2：

// 输入：nums = [1, 2, 3], k = 3
// 输出：2

nums = [1, 1, 1], k = 2

// var subarraySum = function (nums, k) {
//     let obj = { 0: 1 }
//     let count = 0
//     let sum = 0
//     for (let num of nums) {
//         sum += num
//         let target = sum - k
//         if (obj[target] !== undefined) {
//             count += obj[target]
//         }
//         obj[sum] = (obj[sum] || 0) + 1
//     }
//     return count
// };

var subarraySum = function (nums, k) {
    let hashObj = { 0: 1 };
    let count = 0;
    let sum = 0;

    for (let num of nums) {
        sum += num;
        let target = sum - k;
        if (hashObj[target] !== undefined) {
            count += hashObj[target]
        }
        hashObj[sum] = (hashObj[sum] || 0) + 1;
    }
    return count;
}

console.log(subarraySum(nums, k));
